#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QDebug>
#include <QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //为控件下载事件拦截器
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->musicImageBox)
    {
        int ImgWidget = ui->musicImageBox->width();
        int ImgHeight = ui->musicImageBox->height();
        // 拦截⿏标进⼊事件
        if(event->type() == QEvent::Enter)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(ui->musicImageBox->x(),ui->musicImageBox->y(),ImgWidget,ImgHeight));
            animation->setEndValue(QRect(ui->musicImageBox->x(),ui->musicImageBox->y() - 2, ImgWidget, ImgHeight));
            animation->start();

            // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
         }
        else if(event->type() == QEvent::Leave)
        {
            // 拦截⿏标离开事件
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(ui->musicImageBox->x(),ui->musicImageBox->y() ,ImgWidget,ImgHeight));
            animation->setEndValue(QRect(ui->musicImageBox->x(),ui->musicImageBox->y() + 2, ImgWidget, ImgHeight));
            animation->start();
            // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });
            return true;
        }
    }

    return QObject::eventFilter(watched , event);
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(const QString &Imagepath)
{
    QString style = "#recMusicImage{background-image:url(" + Imagepath + ");}";
    ui->recMusicImage->setStyleSheet(style);
}


